package com.molyfun.parents.activity.ask.view;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.molyfun.parents.R;
import com.molyfun.parents.activity.record.itemview.BaseRecordView;
import com.molyfun.parents.activity.record.itemview.RecordViewFactory;
import com.molyfun.parents.rest.bean.GetMediaCommentsResponse;
import com.molyfun.parents.rest.bean.RecordResponse;
import com.molyfun.parents.utils.StringUtils;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:10/18/16.
 */
public class CommentCellView extends LinearLayout implements View.OnClickListener{

    protected LayoutInflater layoutInflater;

    private Context context;

    //用于匿名
    private String userId;

    @InjectView(R.id.content)
    TextView mContent;
    @InjectView(R.id.time)
    TextView mTime;
    @InjectView(R.id.attachment_layout)
    LinearLayout mAttachmentLayout;

    String reply = "回复";
    String colon = ": ";

    public CommentCellView(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    public CommentCellView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public CommentCellView(Context context) {
        super(context);
        init(context, null);
    }

    public CommentCellView(Context context, String userId) {
        super(context);
        init(context, null, userId);
    }

    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        layoutInflater.inflate(R.layout.widget_comment_cell, this);
        ButterKnife.inject(this);
    }

    private void init(Context context, AttributeSet attrs, String userId) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        layoutInflater.inflate(R.layout.widget_comment_cell, this);
        this.userId = userId;
        ButterKnife.inject(this);
    }

    /**
     * init view
     *
     */
    public void initData(GetMediaCommentsResponse.MediaCommentsReply replyBean, String userId , boolean isAnonymous) {
        this.userId = userId;
        initData(replyBean, isAnonymous);
    }



    public void initData(GetMediaCommentsResponse.MediaCommentsReply replyBean, boolean isAnonymous){
        if(!TextUtils.isEmpty(replyBean.getTouserid()) && !TextUtils.isEmpty(replyBean.getTousername())){
            if(isAnonymous){
                if(!TextUtils.isEmpty(userId) ){
                    if(replyBean.getTouserid().equals(userId)){
                        replyBean.setTousername("匿名");
                    }else{
                        replyBean.setUsername("匿名");
                    }
                }
            }
            SpannableStringBuilder builder = new SpannableStringBuilder(replyBean.getUsername() + reply + replyBean.getTousername() + colon + replyBean.getContent());

            if(TextUtils.isEmpty(replyBean.getMtype()) || replyBean.getMtype().equals("4")){
                builder.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, R.color.color_base)), 0, replyBean.getUsername().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                builder.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, R.color.color_cell_text_time)), replyBean.getUsername().length(), replyBean.getUsername().length() + reply.length() , Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                builder.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, R.color.color_base)), replyBean.getUsername().length() + reply.length(), replyBean.getUsername().length() + reply.length() + replyBean.getTousername().length() , Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                builder.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, R.color.color_cell_text_time)), replyBean.getUsername().length() + reply.length() + replyBean.getTousername().length(), replyBean.getUsername().length() + reply.length() + replyBean.getTousername().length() + colon.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                builder.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, R.color.color_cell_text_title)), replyBean.getUsername().length() + reply.length() + replyBean.getTousername().length() + colon.length(), replyBean.getUsername().length() + reply.length() + replyBean.getTousername().length() + colon.length() + replyBean.getContent().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                mContent.setText(builder);
            }



        }else{
            if(isAnonymous){
                if(!TextUtils.isEmpty(userId) ){
                    if(replyBean.getUserid().equals(userId)){
                        replyBean.setUsername("匿名");
                    }
                }
            }
            if(TextUtils.isEmpty(replyBean.getMtype()) || replyBean.getMtype().equals("4")){
                SpannableStringBuilder builder = new SpannableStringBuilder(replyBean.getUsername() + colon + replyBean.getContent());
                builder.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, R.color.color_base)), 0, replyBean.getUsername().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                builder.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, R.color.color_cell_text_time)), replyBean.getUsername().length(), replyBean.getUsername().length() + colon.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                builder.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, R.color.color_cell_text_title)), replyBean.getUsername().length() + colon.length(), replyBean.getUsername().length() + colon.length() + replyBean.getContent().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                mContent.setText(builder);
            }

        }

        mAttachmentLayout.removeAllViews();
        if(!TextUtils.isEmpty(replyBean.getMtype()) && !replyBean.getMtype().equals("4")){
            BaseRecordView recordView = RecordViewFactory.getView(context, new MediaCommentRecordBeanWapper(replyBean));
            mAttachmentLayout.addView(recordView);
            mContent.setText(replyBean.getUsername() + colon);
        }


        if(replyBean.getCreatedatetime() != 0l){
            mTime.setText(StringUtils.formatDate(replyBean.getCreatedatetime()));
        }
    }

    public static class MediaCommentRecordBeanWapper extends RecordResponse.RecordBean{
        public MediaCommentRecordBeanWapper(GetMediaCommentsResponse.MediaCommentsReply replyBean){
            setUrl(replyBean.getUrl());
            setCover(replyBean.getCover());
            setTimelen(replyBean.getTimelen());
            //1 图片 2视频 3音频 4文字
            if(!TextUtils.isEmpty(replyBean.getMtype())){
                setType(Integer.parseInt(replyBean.getMtype()));
            }
        }
    }

    /**
     * init view
     *
     */
    public void initData(GetMediaCommentsResponse.MediaCommentsReply replyBean) {
        initData(replyBean, false);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){

        }
    }

}